Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = _default;

var _api = _interopRequireDefault(require("../../../../utils/api")), _util2 = _interopRequireDefault(require("../../../../public/utils/util")), _util3 = _interopRequireDefault(require("../../../../utils/util"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _objectSpread(t) {
    for (var e = 1; e < arguments.length; e++) {
        var o = null != arguments[e] ? arguments[e] : {}, i = Object.keys(o);
        "function" == typeof Object.getOwnPropertySymbols && (i = i.concat(Object.getOwnPropertySymbols(o).filter(function(e) {
            return Object.getOwnPropertyDescriptor(o, e).enumerable;
        }))), i.forEach(function(e) {
            _defineProperty(t, e, o[e]);
        });
    }
    return t;
}

function _defineProperty(e, t, o) {
    return t in e ? Object.defineProperty(e, t, {
        value: o,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = o, e;
}

var LIMIT = 30, tagReg = /<style (.+?)>(.+?)<\/style>/gim;

function _default() {
    var s = null, i = 0, a = !1;
    return {
        init: function(e) {
            (s = e).setData({
                followLists: []
            }), this.loadRecord(!0), s.$addListener("niche.record.update", function(e) {
                var t = e.content, o = e.alarmtime, i = e.data, a = e.issystem, r = void 0 === a ? 1 : a, n = s.data, l = n.followLists, d = n.followCount;
                if (l) {
                    var u = l.find(function(e) {
                        return e.id === i;
                    }), c = {
                        content: t,
                        alarmtime: o,
                        contents: _util3.default.regExpSeparateLabels(t, tagReg)
                    };
                    u ? Object.assign(u, c) : (l.unshift(_objectSpread({
                        id: i,
                        issystem: r,
                        createtime: new Date(),
                        time: _util2.default.formatTimeStamp(new Date().getTime() - 1e3)
                    }, c)), d = +d + 1), s.setData({
                        followLists: l,
                        followCount: d
                    });
                }
            }, !1), Object.assign(s, {
                operaRecord: function(t) {
                    var o = this, i = [ this.editRecord, this.delRecord ];
                    wx.showActionSheet({
                        itemList: [ "编辑记录", "删除" ],
                        success: function(e) {
                            i[e.tapIndex].call(o, t);
                        }
                    });
                },
                editRecord: function(e) {
                    var t = e.currentTarget.dataset.index;
                    this.$put("niche.add.record", {
                        query: this.query
                    }), this.$put("record.data", this.data.followLists[t]), this.$route("/pages/radar/nichedetail/addRecord/addRecord?mode=edit");
                },
                delRecord: function(e) {
                    var t = this, o = e.currentTarget.dataset, i = o.index, a = o.followid;
                    wx.showModal({
                        content: "确认删除该跟进记录",
                        confirmColor: "#e94f4f",
                        confirmText: "删除",
                        success: function(e) {
                            e.confirm && t.handleDelteRecord(i, a);
                        }
                    });
                },
                handleDelteRecord: function(i, e) {
                    _api.default.minicard.business.delfollow({
                        loading: "删除中...",
                        data: {
                            followid: e,
                            cardid: s.query.cardid
                        }
                    }).then(function() {
                        var e = s.data, t = e.followLists, o = e.followCount;
                        t.splice(i, 1), s.setData({
                            followLists: t,
                            followCount: o - 1
                        });
                    });
                }
            });
        },
        onPullDownRefresh: function() {
            this.loadRecord(!0);
        },
        onReachBottom: function() {
            a || this.loadRecord();
        },
        loadRecord: function(a) {
            var r = this;
            a && (i = 0), _api.default.minicard.business.getfollow({
                data: _objectSpread({
                    offset: i,
                    limit: LIMIT
                }, s.query, {
                    tocardid: s.query.cardid,
                    cardid: s.data.card.cardid
                }),
                complete: function() {
                    wx.hideLoading(), wx.stopPullDownRefresh();
                }
            }).then(function(e) {
                var t = e.data, o = t.list, i = t.count;
                return r.handleLoadRecord(a, o, i);
            });
        },
        handleLoadRecord: function(e, t, o) {
            t = t.map(function(e) {
                return e.time = _util2.default.formatTimeStamp(e.createtime), e.contents = _util3.default.regExpSeparateLabels(e.content, tagReg), 
                e.alarmtime = e.alarmtime ? e.alarmtime.slice(0, -3) : "", e;
            }), s.setData({
                followCount: o,
                followLists: e ? t : s.data.followLists.concat(t)
            }), a = t.length < LIMIT, i += LIMIT;
        }
    };
}